import { createRouter, createWebHistory } from 'vue-router'
import { ElMessage } from 'element-plus'

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/Login.vue'),
    meta: { requiresAuth: false }
  },
  {
    path: '/digital-human',
    name: 'DigitalHuman',
    component: () => import('@/views/DigitalHuman.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/tts',
    name: 'TTS',
    component: () => import('@/views/TTS.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/musetalk',
    name: 'MuseTalk',
    component: () => import('@/views/MuseTalk.vue'),
    meta: { requiresAuth: true }
  },
  {
    path: '/history',
    name: 'History',
    component: () => import('@/views/History.vue'),
    meta: { requiresAuth: true }
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

// 路由守卫
router.beforeEach((to, from, next) => {
  // 检查本地存储中的登录状态
  const isLoggedIn = localStorage.getItem('isLoggedIn') === 'true'
  
  if (to.meta.requiresAuth && !isLoggedIn) {
    ElMessage.warning('请先登录')
    next('/login')
  } else {
    next()
  }
})

export default router
